using System.Drawing;
using System;

namespace NGridDrawer.Partitions
{
    /// <LastUpdate author="Blaise Braye" Date="15/06/2008 17:01"/>
    /// <summary>
    /// this Static class provide some partition helpers
    /// </summary>
    public static class PartitionBuilder
    {
        /// <summary>
        /// This helper method provide a reusable way to partition a grid of data
        /// </summary>
        /// <param name="graphic">graphics used to paint</param>
        /// <param name="clipHeights">defines the maximum bounds for height of each partitionLevel</param>
        /// <param name="clipWidths">define the maximum bounds for width of each partitionColumn</param>
        /// <param name="printOrder">order in which partitionGrid must order partitions</param>
        /// <param name="drawer">a drawer which is able to paint a grid of data</param>
        /// <returns>a partition grid of the tabular grid source</returns>
        /// <see cref="NGridDrawer.Partitions.PartitionGrid"/>
        public static PartitionGrid BuildPartition(
            Graphics graphic,
            float[] clipHeights, float[] clipWidths,
            PartitionOrder printOrder, IPartitionDrawer drawer)
        {
            return BuildPartition(graphic, 0, clipHeights, clipWidths, printOrder, drawer);
            
        }

        /// <summary>
        /// This helper method provide a reusable way to partition a grid of data
        /// </summary>
        /// <param name="graphic">graphics used to paint</param>
        /// <param name="rowHeightIncr">this attribute fixes rows heights in native drawer</param>
        /// <param name="clipHeights">defines the maximum bounds for height of each partitionLevel</param>
        /// <param name="clipWidths">define the maximum bounds for width of each partitionColumn</param>
        /// <param name="printOrder">order in which partitionGrid must order partitions</param>
        /// <param name="drawer">a drawer which is able to paint a grid of data</param>
        /// <returns>a partition grid of the tabular grid source</returns>
        /// <see cref="NGridDrawer.Partitions.PartitionGrid"/>
        public static PartitionGrid BuildPartition(Graphics graphic, float rowHeightIncr, float[] clipHeights, float[] clipWidths, PartitionOrder printOrder, IPartitionDrawer drawer)
        {
            // calculate rows height of the grid according to the drawer
            var rowsHeight = new float[drawer.RowCount];

            for (int i = 0; i < rowsHeight.Length; i++)
                rowsHeight[i] = drawer.RowHeight(graphic, i) + rowHeightIncr;

            // calculate columns widths of the grid according to the drawer
            var columnsWidth = new float[drawer.ColumnCount];

            for (int i = 0; i < columnsWidth.Length; i++)
                columnsWidth[i] = drawer.ColumnWidth(graphic, i);

            var rowHeaderWith = drawer.RowHeaderWidth(graphic);

            // build and return the set of partitions
            return
                new PartitionGrid(
                    rowsHeight,
                    columnsWidth, rowHeaderWith,
                    printOrder,
                    clipHeights, clipWidths);

        }
    }
}